package com.bjb.vr.common.service;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjb.vr.common.ao.UmsAccountAo;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.dto.ResFavorites;
import com.bjb.vr.common.dto.ResPlaycontent;
import com.bjb.vr.common.dto.UmsAccount;
import com.bjb.vr.common.mapper.ResPlaycontentMapper;
import com.bjb.vr.common.result.BaseResult;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.utils.DateTimeUtil;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.bjb.vr.common.vo.ResLocalVo;
import com.bjb.vr.common.vo.ResPlaycontentVo;
import com.bjb.vr.common.vo.UmsAccountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;

/**
 * @author admin
 * @description 针对表【res_playcontent】的数据库操作Service实现
 * @createDate 2022-03-18 18:00:07
 */
@Service
public class ResPlaycontentService extends ServiceImpl<ResPlaycontentMapper, ResPlaycontent> {

    @Autowired
    UmsAccountService umsAccountService;
    @Autowired
    ResPlaycontentMapper playcontentMapper;

    /**
     * 根据条件查询课集里面课程
     *
     * @param playId
     * @return
     */
    public BaseResult<ResPlaycontentVo> queryPlayContent(Integer playId) {
        UmsAccountVo umsAccountVo = (UmsAccountVo) ThreadLocalUtil.get(ThreadLocalConstant.USER_ACCOUNT_INFO);
        List<ResPlaycontentVo> resPlaycontents = playcontentMapper.selectPlayConByPlayId(playId, umsAccountVo.getTenantId());
        buildNeedInfo(resPlaycontents);
        return CommonResult.Ok().setResult(resPlaycontents);
    }


    private void buildNeedInfo( List<ResPlaycontentVo> resPlaycontents) {
        resPlaycontents.forEach(resPlaycontentVo -> {
            try {
//                if(resPlaycontentVo.getContValidStartDate() == null && resPlaycontentVo.getContValidEndDate() == null){
//                    resPlaycontentVo.setContentValidState(1);
//                }else{
//                    LocalDate nowDate = DateTimeUtil.getLocalDate(new Date());
//                    if(nowDate.compareTo(resPlaycontentVo.getContValidStartDate())>=0 && nowDate.compareTo(resPlaycontentVo.getContValidEndDate()) <=0){
//                        resPlaycontentVo.setContentValidState(1);
//                    }else{
//                        resPlaycontentVo.setContentValidState(2);
//                    }
//                }
                LocalDate nowDate = DateTimeUtil.getLocalDate(new Date());
                if(resPlaycontentVo.getContValidStartDate() == null){//起始时间为空
                    resPlaycontentVo.setContentValidState(2);
                }else if (resPlaycontentVo.getContValidEndDate() == null){//起始时间不为空,结束时间为空
                    if (nowDate.compareTo(resPlaycontentVo.getContValidStartDate())>=0 ) {
                        resPlaycontentVo.setContentValidState(1);
                    } else {
                        resPlaycontentVo.setContentValidState(2);
                    }
                }else{//起始时间不为空,结束时间不为空
                    if(nowDate.compareTo(resPlaycontentVo.getContValidStartDate())>=0 && nowDate.compareTo(resPlaycontentVo.getContValidEndDate()) <=0){
                        resPlaycontentVo.setContentValidState(1);
                    }else{
                        resPlaycontentVo.setContentValidState(2);
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    }

    /**
     * 新增课集里面课程
     *
     * @param resPlaycontents
     * @param authorization
     * @return
     */
    public boolean addPlayContent(List<ResPlaycontent> resPlaycontents, String authorization) {
        // 查询用户信息
        UmsAccountAo umsAccountAo = UmsAccountAo.builder().authorization(authorization).build();
        UmsAccountVo accountInfo = umsAccountService.getAccountInfo(umsAccountAo);
        if (Objects.isNull(accountInfo)) {
            return false;
        }
        return saveBatch(resPlaycontents);
    }


    /**
     * 删除课集里面课程
     *
     * @param playid
     * @param authorization
     * @return
     */
    public boolean removePlayContent(Long playid, String authorization) {
        // 查询用户信息
        UmsAccountAo umsAccountAo = UmsAccountAo.builder().authorization(authorization).build();
        UmsAccountVo accountInfo = umsAccountService.getAccountInfo(umsAccountAo);
        if (Objects.isNull(accountInfo)) {
            return false;
        }
        //根据playid删除对应的课程内容
        HashMap<String, Object> map = new HashMap<>();
        map.put("play_id", playid);
        return removeByMap(map);
    }


    /**
     * 批量删除课集里面课程（给经波调用）
     *
     * @param playIds
     * @return
     */
    public boolean removeByPlayIds(String playIds) {
        //根据playid删除对应的课程内容
        String[] idArray = playIds.split(",");
        Long[] intArray = Convert.toLongArray(idArray);
        //批量删除课程集的内容表
        for (String playId : idArray) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("play_id", playId);
        }
        return true;
    }


}




